package es

import (
	"github.com/olivere/elastic/v7"
)

//  -------------------------------------------- Aggs Metrics -------------------------------------------
// min 最小值
func (m *Model) Min(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewMinAggregation().Field(field))
}

// max 最大值
func (m *Model) Max(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewMaxAggregation().Field(field))
}

// avg 平均值
func (m *Model) Avg(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewAvgAggregation().Field(field))
}

// sum 求和值
func (m *Model) Sum(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewSumAggregation().Field(field))
}

// value count 个数和
func (m *Model) ValueCount(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewValueCountAggregation().Field(field))
}

// cardinality 去重个数和
func (m *Model) Cardinality(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewCardinalityAggregation().Field(field))
}

// stats (min、max、sum、count、avg) 批量统计
func (m *Model) Stats(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewStatsAggregation().Field(field))
}

// extend stats 拓展统计聚合
func (m *Model) ExtendedStats(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewExtendedStatsAggregation().Field(field))
}

// percentiles 百分比统计
func (m *Model) Percentiles(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewPercentilesAggregation().Field(field))
}

// percentiles rank 百分比统计排行
func (m *Model) PercentileRanks(name string, field string, values ...float64) *Model {
	return m.SubAggregation(name, elastic.NewPercentileRanksAggregation().Field(field).Values(values[0]))
}

// top hits 最高匹配权值聚合
func (m *Model) TopHits(name string, size int, field ...string) *Model {
	fsc := elastic.NewFetchSourceContext(true).Include(field[0])
	return m.SubAggregation(name, elastic.NewTopHitsAggregation().FetchSourceContext(fsc).Size(size))
}

// weighted avg 聚合权重平均
func (m *Model) WeightedAvg(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewWeightedAvgAggregation().Value(&elastic.MultiValuesSourceFieldConfig{
		FieldName: field,
	}))
}

// Geo Bounds Aggregation 地理边界聚合
func (m *Model) GeoBounds(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewGeoBoundsAggregation().Field(field))
}

// Geo Centroid Aggregation 地理重心聚合
func (m *Model) GeoCentroid(name string, field string) *Model {
	return m.SubAggregation(name, elastic.NewGeoCentroidAggregation().Field(field))
}